﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AcsCommon;
using Newtonsoft.Json;
using System.Collections;
using Microsoft.JScript;
using System.Data;

namespace YiShi.Exam
{
    public partial class ExamQuestionEdit : System.Web.UI.Page
    {
        DbAccess db;
        string titleTrTemplate = @"
<tr name='TitleTr' id='{0}'>
    <td style='border: 1px solid #c5dbec; background-color: #f5f8f9;' colspan='2' align='left'>
        <font style='font-size: 11pt; font-weight: bold'>{1}</font>
    </td>
</tr>";
        string questionTrTemplate = @"
<tr {0} style='display:{1}'>
    <td class='tdclass'>
        <input type='radio' name='Question' value='{2}' sortindex='{3}'/>
    </td>
    <td class='tdclass' align='left'>
        <div style='overflow: auto; text-align: left; width: 400px; height: 100px;'>
            {4}
        </div>
    </td>
</tr>";
        protected string tr;
        protected string html;

        public ExamQuestionEdit()
        {
            tr = "";
            html = "";
            db = DbAccess.Get("Admin");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            object jsonobj = null;
            try
            {
                string action = Request["ActionType"] == null ? "" : Request["ActionType"];
                switch (action.ToLower())
                {
                    case "add":
                        jsonobj = Add();
                        break;
                    case "remove":
                        jsonobj = Remove();
                        break;
                    case "updatesortindex":
                        jsonobj = UpdateSortIndex();
                        break;
                    case "dataedit":
                        DataEdit();
                        break;
                }
            }
            catch (Exception e1)
            {
                jsonobj = new
                {
                    IsSuccess = "false",
                    Message = e1.Message,
                    Source = e1.Source,
                    StackTrace = e1.StackTrace
                };
            }
            finally
            {
                if (jsonobj != null)
                {
                    Response.Write(JsonConvert.SerializeObject(jsonobj));
                    Response.End();
                }
            }
        }

        private object Remove()
        {
            string id = Request["Id"];
            string questionid = Request["QuestionId"];
            string sql = "delete from ExamQuestion where (ExamId = '{0}') and (QuestionId = '{1}')";
            db.ExecuteSql(Sql.Format(sql, id, questionid), 1);
            return new
            {
                IsSuccess = "true"
            };
            throw new NotImplementedException();
        }

        private object UpdateSortIndex()
        {
            string id = Request["Id"];
            string sortindex = Request["SortIndex"];
            string questionid = Request["QuestionId"];
            string sql = "update ExamQuestion set SortIndex = '{0}' where (ExamId = '{1}') and (QuestionId = '{2}')";
            db.ExecuteSql(Sql.Format(sql, sortindex, id, questionid), 1);
            return new
            {
                IsSuccess = "true"
            };
        }

        private object Add()
        {
            string id = Request["Id"];
            string sortindex = Request["SortIndex"];
            string questionid = Request["QuestionId"];
            string sql = "insert into ExamQuestion(ExamId, QuestionId, SortIndex) values ('{0}', '{1}', '{2}')";
            db.ExecuteSql(Sql.Format(sql, id, questionid, sortindex), 1);
            return new
                {
                    IsSuccess = "true"
                };
        }

        private void DataEdit()
        {
            DbEnum de = DbEnum.Get(db, "Question.Type");
            Hashtable titleTrHtmls = new Hashtable();
            for (int i = 0; i < de.Count; i++)
                titleTrHtmls.Add(de[i].Value, String.Format(titleTrTemplate, de[i].Value, de[i].Text));

            tr = String.Format(questionTrTemplate, "id='QuestionTemplate'", "none", "", "", "");

            string id = Request["Id"];
            string sql = @"
select Question.*, ExamQuestion.SortIndex
from ExamQuestion inner join
     Question on ExamQuestion.QuestionId = Question.Id
where (ExamQuestion.ExamId = '{0}')
order by ExamQuestion.SortIndex";
            sql = Sql.Format(sql, id);
            DataTable dt = this.db.QueryDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                titleTrHtmls[dr.GetValue("Type")] += String.Format(questionTrTemplate, "", "", dr.GetValue("Id"), dr.GetValue("SortIndex"), GlobalObject.unescape(dr.GetValue("Body")));
            }
            for (int i = 0; i < de.Count; i++)
                html += titleTrHtmls[de[i].Value];
        }
    }
}
